/**
 * @Title: IssueDaoJpa.java 
 * @Description: TODO
 * @author <a href="mailto:psammead@126.com">Psammead</a>
 * @date 2011-7-16 下午02:53:37 
 * @version 1.0
 * Copyright 2011 by Psammead
 */
package com.psammead.dms.project.dao.jpa;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.psammead.dms.common.core.bo.Page;
import com.psammead.dms.common.core.dao.jpa.GenericDaoJpa;
import com.psammead.dms.project.dao.IssueDao;
import com.psammead.dms.project.entity.Issue;

/**
 * @ClassName: IssueDaoJpa
 * @Description: TODO
 * @author <a href="mailto:psammead@126.com">psammead</a>
 * @version 1.0
 * @date 2011-7-16
 */
@Repository("issueDao")
public class IssueDaoJpa extends GenericDaoJpa<Issue, Long> implements
		IssueDao {
	/**
	 * 构造函数，继承父类的构造
	 * <p>Title: </p>
	 * <p>Description: </p>
	 */
	public IssueDaoJpa(){
		super(Issue.class);
	}
	
	/**
	 * 拼接查询条件
	 * @Title: sqlBuffer
	 * @Description: TODO
	 * @param paramMap
	 * @return StringBuffer
	 * @throws
	 */
	protected StringBuffer sqlMaker(Map<String, Object> paramMap){
		StringBuffer sb=new StringBuffer();
		if(!paramMap.keySet().isEmpty()){
			for(String key:paramMap.keySet()){
				if("title".equals(key)){
					sb.append(" and title like :title");
					paramMap.put(key, "%"+(String)paramMap.get(key)+"%");
				}
				if("status".equals(key)){
					sb.append(" and status = :status");
				}
				if("project".equals(key)){
					sb.append(" and project = :project");
				}
				if("creator".equals(key)){
					sb.append(" and creator = :creator");
				}
				if("updater".equals(key)){
					sb.append(" and updater = :updater");
				}
				if("closer".equals(key)){
					sb.append(" and closer = :closer");
				}
				if("charger".equals(key)){
					sb.append(" and charger = :charger");
				}
				if("creatTimeBefore".equals(key)){
					sb.append(" and creatTime <:creatTimeBefore");
				}
				if("creatTimeAfter".equals(key)){
					sb.append(" and creatTime <:creatTimeAfter");
				}
				if("planCloseTimeBefore".equals(key)){
					sb.append(" and planCloseTime <:planCloseTimeBefore");
				}
				if("planCloseTimeAfter".equals(key)){
					sb.append(" and planCloseTime <:planCloseTimeAfter");
				}
				if("actCloseTimeBefore".equals(key)){
					sb.append(" and actCloseTime <:actCloseTimeBefore");
				}
				if("actCloseTimeAfter".equals(key)){
					sb.append(" and actCloseTime <:actCloseTimeAfter");
				}
			}
		}
		return sb;
	}

	@Override
	public List<Issue> queryResultList(Map<String, Object> paramMap,
			LinkedHashMap<String, String> sortMap) {
		return this.queryResultList(new JsqlBuilder(){
			@Override
			public String buildJSql(Map<String, Object> paramMap) {
				StringBuffer sb= new StringBuffer();
				sb.append("select o from Issue o where 1=1");
				sb.append(sqlMaker(paramMap));
				return sb.toString();
			}
		}, paramMap, sortMap);
	}

	@Override
	public Page<Issue> queryResultPage(Map<String, Object> paramMap,
			LinkedHashMap<String, String> sortMap, int pageNo, int pageSize) {
		return this.queryResultPage(new SqlBuilder(){
			@Override
			public String buildJSql(Map<String, Object> paramMap) {
				StringBuffer sb= new StringBuffer();
				sb.append("select o from Issue o where 1=1");
				sb.append(sqlMaker(paramMap));
				return sb.toString();
			}
			@Override
			public String buildCountSql(Map<String, Object> paramMap) {
				StringBuffer sb= new StringBuffer();
				sb.append("select count(o.id) from Issue o where 1=1");
				sb.append(sqlMaker(paramMap));
				return sb.toString();
			}
		}, paramMap, sortMap, pageNo, pageSize);
	}

}
